<?php
namespace app\http;


use app\http\Redis;
use app\servers\MembershipCardServer;
use think\worker\Server;

class webSocker extends Server
{
    protected $socket = 'websocket://0.0.0.0:2348';
    public function onWorkerStart($worker)
    {

        $ws_worker=new \Workerman\Worker('tcp://0.0.0.0:2347');

           while (true){
            $config = [
                'host' => '192.168.2.3',  //服务器连接地址。默认='127.0.0.1'
                'port' => 6379,  //端口号。默认='6379'
                'password' => '',  // 连接密码，如果有设置密码的话
                'db' => 1,   //缓存库选择。默认0
                'timeout' => 10  // 连接超时时间（秒）。默认10
            ];
            $_redis = new Redis($config);
            $_res = $_redis::lpop('HENG_TONG_DOWN_FACE_SERVER_KEYS');

            if($_res){
                $_comd = json_decode($_res, true);

                if(array_key_exists('cmd', $_comd)){
                    switch ($_comd['cmd']){
                        case 'AH'://下发用户信息
                        {
                            if(array_key_exists('data', $_comd)){
                                if(array_key_exists('user_id', $_comd['data'])){
                                    //$_comd['data']['user_id']

                                    //卡号
//                                    $card = $res['card_code'];
                                    var_dump($_comd);
                                    $card = dechex($_comd['data']['card_code']);
                                    if(strlen($card) < 8){
                                        $card2 =  str_pad($card,8,0,STR_PAD_LEFT);
                                    }
                                    //图片
                                    $path = 'http://192.168.2.3/attachment/'.$_comd['data']['userpic'];

                                    $imgs =  file_get_contents($path);
                                    $imgsez = $_comd['data']['file_size'];
                                    $img_len_1 = bin2hex($imgs);
                                    //计算长度
                                    $_len = $imgsez+3+4+1+1+2;
                                    $img_len = dechex($_len);

                                    $_member = new MembershipCardServer();
                                    $str_2 =$_member->reStrings($img_len);

                                    if(strlen($str_2) < 6){
                                        $imgLen =  str_pad($str_2,6,0,STR_PAD_RIGHT);
                                    }
                                    //CRC
                                    $result2 = '444D' . $imgLen .$card2.$img_len_1;
                                    $value = '';
                                    for($i=0;$i<strlen($result2);$i+=2){
                                        $char = substr($result2,$i,2);
                                        if(!$value){
                                            $value = hexdec($char);
                                        }else{
                                            $value ^= hexdec($char);
                                        }
                                    }
                                    //发送
                                    $result5 =$result2.dechex($value).'21';
                                    if($ws_worker->send(pack('H*',$result5)) == true){
                                        //成功逻辑
                                        $data = ['code'=> 0,'msg'=>'下发成功'];//success
                                        $_redis::lpush('IMG_ISSUED_SUCCESS',json_encode($data));
                                    }else {
                                        $data = ['code' => 1, 'msg' => '下发失败'];
                                        $_redis::lpush('IMG_ISSUED_ERROR',json_encode($data));
                                    }
                                }
                            }
                            break;
                        }
                        default : {
                            //默认处理
                            break;
                        }
                    }
                }


                sleep(60);
            }

            sleep(3);
        }

    }

    public function onConnect($connection)
    {

    }

    public function onMessage($connection ,$data)
    {






    }

    // 当客户端的连接上发生错误时触发
    public function onErroe($connection,$code,$msg)
    {
        echo "error $code $msg\n";
    }




}